package com.yeyks.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.websocket.server.ServerEndpointConfig;

/**
 * @Author fuchangshuai
 * @date 2019/4/8 12:42 PM
 */
@Component
@Lazy(false)
public class MyApplicationContextAware extends ServerEndpointConfig.Configurator implements ApplicationContextAware {

    //  通过手动注入applicationContext上下文获取Bean
    private static volatile BeanFactory context;

    @Override
    public <T> T getEndpointInstance(Class<T> clazz) {
        return context.getBean(clazz);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        MyApplicationContextAware.context = applicationContext;
    }
}
